package ru.gelin.android.keyboard;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import static ru.gelin.android.keyboard.Tag.TAG;

public class MainActivity extends ListActivity {

    static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

    static final int DESCRIPTION_POS = 0;
    static final int ON_DOCK_CHECKBOX_POS = 1;
    static final int NOTIFICATION_CHECKBOX_POS = 2;
    static final int SHORTCUT_BUTTON_POS = 3;
    static final int MAX_POS = 4;

    enum ListItemType {
        DESCRIPTION, BUTTON, CHECKBOX;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new MainListAdapter());
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
        MainPreferences preferences = new MainPreferences(this);
        switch (position) {
            case SHORTCUT_BUTTON_POS:
                installShortcut();
                break;
            case ON_DOCK_CHECKBOX_POS:
                preferences.setOnDockListen(!preferences.isOnDockListen());
                setCheckboxChecked(view, preferences.isOnDockListen());
                break;
            case NOTIFICATION_CHECKBOX_POS:
                preferences.setShowNotification(!preferences.isShowNotification());
                setCheckboxChecked(view, preferences.isShowNotification());
                toggleNotification(preferences.isShowNotification());
                break;
        }
    }

    void installShortcut() {
        Log.i(TAG, "installing shortcut");
        Intent intent = Shortcut.getExtraShortcutIntent(this);
        intent.setAction(ACTION_INSTALL_SHORTCUT);
        sendBroadcast(intent);
    }

    void toggleNotification(boolean show) {
        if (show) {
            ShowInputMethodPickerNotification.show(this);
        } else {
            ShowInputMethodPickerNotification.hide(this);
        }
    }

    void setCheckboxChecked(View view, boolean checked) {
        CheckBox checkbox = (CheckBox)view.findViewById(R.id.checkbox);
        checkbox.setChecked(checked);
    }

    class MainListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return MAX_POS;
        }

        @Override
        public int getViewTypeCount() {
            return ListItemType.values().length;
        }

        @Override
        public int getItemViewType(int position) {
            return getListItemType(position).ordinal();
        }

        ListItemType getListItemType(int position) {
            switch (position) {
                case DESCRIPTION_POS: return ListItemType.DESCRIPTION;
                case ON_DOCK_CHECKBOX_POS: return ListItemType.CHECKBOX;
                case NOTIFICATION_CHECKBOX_POS: return ListItemType.CHECKBOX;
                case SHORTCUT_BUTTON_POS: return ListItemType.BUTTON;
            }
            return null;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ListItemType type = getListItemType(position);
            View view = convertView;
            if (view == null) {
                view = createView(type, parent);
            }
            switch (type) {
                case BUTTON:
                    bindButton(position, view);
                    break;
                case CHECKBOX:
                    bindCheckbox(position, view);
                    break;
            }
            return view;
        }

        View createView(ListItemType type, ViewGroup parent) {
            switch (type) {
                case DESCRIPTION:
                    return getLayoutInflater().inflate(R.layout.description, parent,  false);
                case BUTTON:
                    return getLayoutInflater().inflate(R.layout.button, parent, false);
                case CHECKBOX:
                    return getLayoutInflater().inflate(R.layout.checkbox, parent, false);
            }
            return null;
        }

        void bindButton(int position, View view) {
            TextView label = (TextView)view.findViewById(R.id.label);
            TextView description = (TextView)view.findViewById(R.id.description);
            switch (position) {
                case SHORTCUT_BUTTON_POS:
                    label.setText(R.string.add_shortcut_button);
                    description.setText(R.string.add_shortcut_description);
                    break;
                case ON_DOCK_CHECKBOX_POS:
                    label.setText(R.string.on_dock_button);
                    description.setText(R.string.on_dock_description);
                    break;
                case NOTIFICATION_CHECKBOX_POS:
                    label.setText(R.string.notification_button);
                    description.setText(R.string.notification_description);
                    break;
            }
        }

        void bindCheckbox(int position, View view) {
            bindButton(position, view);
            MainPreferences preferences = new MainPreferences(MainActivity.this);
            switch (position) {
                case ON_DOCK_CHECKBOX_POS:
                    setCheckboxChecked(view, preferences.isOnDockListen());
                    break;
                case NOTIFICATION_CHECKBOX_POS:
                    setCheckboxChecked(view, preferences.isShowNotification());
                    break;
            }
        }

    }

}